package com.shareppy.nomrnaui.util;

import java.lang.reflect.Field;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.util.SparseArray;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.shareppy.norman.core.annotations.Inherited;
import com.shareppy.norman.core.annotations.NotNull;
import com.shareppy.norman.core.entity.AbstractEntity;
import com.shareppy.norman.core.util.EntityUtils;
import com.shareppy.norman_ui.R;
import com.shareppy.utils.ReflectionUtils;
import com.shareppy.utils.StringUtils;

public class UIUtil {

	@SuppressWarnings("unchecked")
	public static <T extends View> T getView(Activity activity, int viewId) {
		return (T) activity.findViewById(viewId);
	}

	@SuppressWarnings("unchecked")
	public static <T extends View> T getView(Dialog dialog, int viewId) {
		return (T) dialog.findViewById(viewId);
	}

	public static <E extends AbstractEntity, A extends Activity> E getEntity(
			A activity, Class<E> clazz, String... fields) {
		E entity = null;
		try {
			entity = ReflectionUtils.newInstance(clazz);
			String[] _fields = fields;
			if (fields == null || fields.length == 0) {
				_fields = EntityUtils.getColumnArray(null, entity);
			}
			Class<?> activityClass = activity.getClass();
			for (String fieldName : _fields) {
				try {
					Field entityField = ReflectionUtils.getAccessibleField(
							clazz, fieldName, Inherited.class);
					Field accessibleField = ReflectionUtils.getAccessibleField(
							activityClass, fieldName, Inherited.class);
					boolean isRequired = entityField
							.getAnnotation(NotNull.class) != null;
					if (accessibleField.getType().isAssignableFrom(
							EditText.class)) {
						EditText editText = (EditText) accessibleField
								.get(activity);
						if (isRequired
								&& StringUtils.isEmpty(editText.getText()
										.toString())) {
							editText.requestFocus();
							editText.setError(activity
									.getString(R.string.required_field));
							return entity;
						} else {
							editText.setError(null);
						}
						entityField.set(entity, editText.getText().toString());
					}

				} catch (NoSuchFieldException e) {
//					e.printStackTrace();
				}
			}
		} catch (InstantiationException e) {
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			e.printStackTrace();
		}
		return entity;
	}

	public static Dialog dialog(Context context, String title,
			int layoutResource) {
		return dialog(context, title, layoutResource, null);
	}

	public static Dialog dialog(Context context, String title,
			int layoutResource, SparseArray<String> datasource) {
		final Dialog dialog = new Dialog(context);
		dialog.setContentView(layoutResource);

		if (datasource != null) {
			for (int i = 0; i < datasource.size(); i++) {
				int key = datasource.keyAt(i);
				TextView text = (TextView) dialog.findViewById(key);
				text.setText(datasource.get(key));
			}
		}
		dialog.setTitle(title);
		dialog.show();
		return dialog;
	}
	
	public static void go2Home(Context context) {
		Intent startMain = new Intent(Intent.ACTION_MAIN);
		startMain.addCategory(Intent.CATEGORY_HOME);
		startMain.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
		context.startActivity(startMain);
	}
}